package com.itheima.common.advice;

import com.itheima.common.entity.Result;
import com.itheima.common.entity.ResultCode;
import com.itheima.common.exception.IhrmException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/6/24 16:27
 *
 * 通用异常处理类（底层使用的是aop编程思想）：
 * 类上加了@ControllerAdvice注解后，会拦截Controller中所有的方法。
 * 当Controller的方法出现异常时，就会执行ExceptionHandler方法
 */

@Slf4j
@ControllerAdvice
public class BaseExceptionAdvice {

    @ExceptionHandler(IhrmException.class)
    @ResponseBody
    public Result handleException(HttpServletRequest request, HttpServletResponse response, IhrmException e) {
       return new Result(e.getStatus(),e.getMessage(),false);
    }

    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public Result handleAuthenicationException(AuthenticationException e){
        return new Result(ResultCode.UNAUTHENTICATED);
    }
}
